package com.uam.utils.file.analysis.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uam.common.exception.UamException;
import com.uam.common.file.FileToStringUtils;
import com.uam.db.model.vo.TaskVO;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class Csv2GeoJson  extends AnalysisParent implements File2GeoJson {
    @Override
    public RetAnalysisVo fileToGeoJsonFile(String fileUrl, String targetUrl, String fileName) throws IOException {
        String data = FileToStringUtils.txt2String(fileUrl);
        if (StringUtils.isBlank(data)) {
            throw UamException.fileTypeError("文件数据解析为空 ！ 文件地址：" + fileUrl);
        }
        if(data.contains("airplaneName")){
            String dataUrl= getFileNameGeoJson(fileName,targetUrl);
            boolean flag=writerToFile(dataToGeoJson(data),dataUrl);
            if(!flag){
                throw UamException.fileTypeError("写入文件错误！ " + fileName);
            }
            return new RetAnalysisVo(2,dataUrl,dataUrl.length(),"{}");

        }
        return toGeoJsonFile(data, targetUrl, fileName);
    }

    public static String dataToGeoJson(String data) {
        String[] dataArray = data.split("\n");
        System.out.println("dataArray.length:"+dataArray.length);
        JSONObject geoJson = new JSONObject();
        geoJson.put("type", "FeatureCollection");
        JSONArray features = new JSONArray();
        int row=dataArray.length;
        for (int i=1;i<row;i++){
            try {
                System.out.println("dataArray.length-------------:"+dataArray[i]);
                TaskVO taskVO = JSONObject.parseObject(dataArray[i], TaskVO.class);
                JSONObject jsonArray = Csv2GeoJson.toGeoJson(taskVO);
                features.add(jsonArray);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        geoJson.put("features", features);
        return geoJson.toJSONString();
    }

    public static JSONObject toGeoJson(TaskVO taskVO) {
        JSONArray getAltitudes = new JSONArray();
        JSONArray supervisory_signal_num = new JSONArray();
        JSONArray supervisory_signal = new JSONArray();
        JSONArray navigation_signal = new JSONArray();
        JSONArray positioning_accuracy = new JSONArray();
        JSONArray conn_num = new JSONArray();
        JSONArray speed = new JSONArray();
        JSONArray communication_signal = new JSONArray();
        JSONArray navigation_signal_num = new JSONArray();

        JSONArray time = new JSONArray();
        JSONArray conflict_point = new JSONArray();
        JSONArray safe_distance = new JSONArray();
        JSONArray communication_signal_num = new JSONArray();
        JSONArray communication_signal_position = new JSONArray();
        JSONArray supervisory_signal_position = new JSONArray();
        JSONArray navigation_signal_position = new JSONArray();

        JSONArray transitPointsIndex = new JSONArray();
        // 创建单个Feature对象
        JSONObject feature = new JSONObject();
        feature.put("type", "Feature");

        // 构建geometry部分
        JSONObject geometry = new JSONObject();
        geometry.put("type", "MultiLineString");

        // 轨迹坐标点数组
        JSONArray coordinates = new JSONArray();
        for (int i = 0; i < taskVO.getTrack().size(); i++) {
            JSONObject point = taskVO.getTrack().getJSONObject(i);
            JSONArray position = point.getJSONArray("position");
            coordinates.add(position);
            getAltitudes.add(point.getDouble("altitude"));
            supervisory_signal_num.add(point.getInteger("supervisory_signal_num"));
            supervisory_signal.add(point.getString("supervisory_signal"));
            navigation_signal.add(point.getString("navigation_signal"));
            communication_signal.add(point.getString("communication_signal"));
            positioning_accuracy.add(point.getString("positioning_accuracy"));
            conn_num.add(point.getInteger("conn_num"));
            speed.add(point.getDouble("speed"));
            time.add(point.getString("time"));
            conflict_point.add(point.getBoolean("conflict_point"));
            safe_distance.add(point.getDouble("safe_distance"));
            communication_signal_num.add(point.getInteger("communication_signal_num"));
            communication_signal_position.add(point.getJSONArray("communication_signal_position"));
            navigation_signal_num.add(point.getInteger("navigation_signal_num"));
            navigation_signal_position.add(point.getJSONArray("navigation_signal_position"));
            supervisory_signal_position.add(point.getJSONArray("supervisory_signal_position"));
            transitPointsIndex.add(i);

        }
        JSONArray heading = new JSONArray();
        heading.add(coordinates);

        geometry.put("coordinates", heading);
        feature.put("geometry", geometry);

        JSONObject properties = new JSONObject();
        properties.put("taskName", taskVO.getTaskName());
        properties.put("airplaneName", taskVO.getAirplaneName());
        properties.put("startPointName", taskVO.getStartPointName());
        properties.put("destinationName", taskVO.getDestinationName());
        properties.put("startTime", taskVO.getStartTime());
        properties.put("mode", taskVO.getMode());
        properties.put("transitPointsName", taskVO.getTransitPointsName());
        properties.put("transitPointsIndex", transitPointsIndex);
        properties.put("taskId", taskVO.getId());

        properties.put("altitudes", getAltitudes);
        properties.put("supervisory_signal_num",supervisory_signal_num);
        properties.put("supervisory_signal", supervisory_signal);
        properties.put("navigation_signal", navigation_signal);
        properties.put("positioning_accuracy", positioning_accuracy);
        properties.put("conn_num", conn_num);
        properties.put("speed", speed);
        properties.put("communication_signal", communication_signal);
        properties.put("navigation_signal_num", navigation_signal_num);
        properties.put("time",  time);
        properties.put("conflict_point", conflict_point);
        properties.put("safe_distance", safe_distance);
        properties.put("communication_signal_num", communication_signal_num);
        properties.put("communication_signal_position",communication_signal_position);
        properties.put("supervisory_signal_position",supervisory_signal_position);
        properties.put("navigation_signal_position",navigation_signal_position);
        properties.put("transitPointsIndex",transitPointsIndex);
        feature.put("properties", properties);
       return feature;
    }

    public static void main(String[] args) throws IOException {
        Csv2GeoJson csv2GeoJson=new Csv2GeoJson();
        RetAnalysisVo c=  csv2GeoJson.fileToGeoJsonFile("C:\\Users\\Administrator\\Desktop\\20241112000000-20241112235959.csv","C:\\Users\\Administrator\\Desktop\\","1111119.geojson");
        System.out.println(c);
        //        String data = FileToStringUtils.txt2String("C:\\Users\\Administrator\\Desktop\\20241112000000-20241112235959.csv");
//        System.out.println(dataToGeoJson(data));
    }
}
